package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.beancontext.BeanContext;
import java.util.List;

/**
 * 地址簿管理
 */
@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    /**
     * 新增地址簿
     */
    public void save(AddressBook addressBook) {
        addressBookMapper.insert(addressBook);
    }

    /**
     * 查询地址簿列表
     */
    public List<AddressBook> list(AddressBook addressBook) {
        return addressBookMapper.getByUserId(addressBook);
    }

    /**
     * 查询地址簿单条
     * @return
     */
    public AddressBook getById(String label, Long id) {
        AddressBook addressBook =   addressBookMapper.getById(id);
        return addressBook;
    }

    /**
     *编辑地址簿
     */
    public void update(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }

    /**
     * 删除地址簿单条
     */
    public void delete(Long id) {
        addressBookMapper.delete(id);
    }

    /**
     * 设为默认地址
     */
    public void setDefault(AddressBook addressBook) {
//        addressBookMapper.update(addressBook);
        //查询所有地址
        AddressBook addressBook1 = new AddressBook();
        addressBook1.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addressBookMapper.getByUserId(addressBook1);
        for (AddressBook book : list) {
            if(book.getId() == addressBook.getId()){
                book.setIsDefault(StatusConstant.ENABLE);
            }else{
                book.setIsDefault(StatusConstant.DISABLE);

            }
        }
        addressBookMapper.batchUpdate(list);

    }

}
